<template>
    <div class="container">
        <el-form
            :model="ruleForm"
            ref="ruleForm"
            :rules="rules"
            :label-position="labelPosition"
            label-width="150px"
            class="demo-ruleForm"
        >
            <div style="margin-top:20px">
                <h3 检验检测</h3>
                <h3 style="margin-bottom: 20px" v-show="$store.state.zangw=='1'">བརྟག་བཤེར་བཤེར་འཇལ་</h3>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="检测机构：" >
                          <div slot="label">检测机构</div>
                          <div slot="label" style="line-height: 0px" v-show="$store.state.zangw=='1'">བཤེར་འཇལ་སྒྲིག་གཞི།</div>
                            <el-input
                                placeholder="请输入内容"
                                v-model="ruleForm.organization"
                                style="width:300px"
                            ></el-input>
                        </el-form-item>
                        <el-form-item label="检测标准：" >
                          <div slot="label">检测标准</div>
                          <div slot="label" style="line-height: 0px" v-show="$store.state.zangw=='1'">བཤེར་འཇལ་ཚད་གཞི་ </div>
                            <el-select
                                v-model="standard"
                                @change="changeStandard"
                                placeholder="请选择"
								value-key="id"
                                style="width:300px"
                            >
                                <el-option
                                    v-for="item in standardList"
                                    :key="item.id"
                                    :label="item.checkName"
                                    :value="item"
                                ></el-option>
                            </el-select>
                            <el-button style="margin-left:20px" @click="add">添加</el-button>
                        </el-form-item>
                        <el-form-item label="检测结果：" >
                          <div slot="label">检测结果</div>
                          <div slot="label" style="line-height: 0px" v-show="$store.state.zangw=='1'">བཤེར་འཇལ་མཇུག་འབྲས་</div>
                            <el-radio v-model="ruleForm.detectionResult" label="合格">合格</el-radio>
                            <el-radio v-model="ruleForm.detectionResult" label="不合格">不合格</el-radio>
                        </el-form-item>
                        <el-form-item label="检验标准文件："  style="width:500px">
                          <div slot="label">检验标准文件</div>
                          <div slot="label" style="line-height: 0px" v-show="$store.state.zangw=='1'">བརྟག་བཤེར་ཚད་གཞིའི་ཡིག་ཆ།</div>
                            <upload-img ref="uploadChang" :maxNum="8" :files="listUrl"></upload-img>
                        </el-form-item>
                    </el-col>
                    <el-col :span="10">
                        <el-form-item label="检测负责人：" >
                          <div slot="label">检测负责人</div>
                          <div slot="label" style="line-height: 0px" v-show="$store.state.zangw=='1'">བཤེར་འཇལ་འགན་ཁུར་བ་</div>
                            <el-select
                                v-model="user"
                                @change="changeUser"
								value-key="id"
                                placeholder="请选择"
                                style="width:300px"
                            >
                                <el-option
                                    v-for="item in userList"
                                    :key="item.id"
                                    :label="item.name"
                                    :value="item"
                                ></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="检测时间：">
                          <div slot="label">检测时间</div>
                          <div slot="label" style="line-height: 0px" v-show="$store.state.zangw=='1'">བཤེར་འཇལ་དུས་ཚོད།</div>
                            <el-date-picker
                                v-model="ruleForm.time"
                                type="date"
                                value-format="yyyy-MM-dd"
                                placeholder
                                style="width:300px"
                            ></el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>
            </div>
            <el-form-item class="footerFixed" label-width="0px">
                <el-button type="primary" @click="submitrule('ruleForm')">提交</el-button>
                <el-button @click="backPage">返回</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import { processDetail_findById, check_findByAll, user_findByAll,detection_save} from '@/request/api';
import uploadImg from '../../../common/uploadImg';
export default {
    components: {
        uploadImg
    },
    data() {
        return {
            user: '',
            standard: '',
            radio: 1,
            standardList: [],
            userList: [],
            // ifshow:true,
            imgurl: '',
            show: true,
            labelPosition: 'right',
            // showForm: false,
            ruleForm: {},
            detail: {},
            listUrl: '',
            id: '',
            rules: {
                organization: [{ required: true, message: '不能为空', trigger: 'change' }],
                // standard: [{ required: true, message: '不能为空', trigger: 'change' }],
                userName: [{ required: true, message: '不能为空', trigger: 'change' }],
                detectionResult: [{ required: true, message: '不能为空', trigger: 'change' }],
                time: [{ required: true, message: '不能为空', trigger: 'change' }],
            }
        };
    },
    mounted() {
        this.id = this.$route.query.id;
        this.ruleForm.relevanceId = this.$route.query.id;
        this.ruleForm.type = this.$route.query.type;
        if (this.id == undefined) {
        } else {
            this.init();
        }
    },
    methods: {
        init() {
            processDetail_findById(this.id).then((res) => {
                this.detail = res.data;
            });
            check_findByAll('').then((res) => {
                this.standardList = res.data;
            });
            user_findByAll('').then((res) => {
                this.userList = res.data;

            });
        },
        add() {
            this.$router.push({
                path: '../checkingBasis/added'
            });
        },
        changeUser(ele) {
            this.ruleForm.userId = ele.id;
            this.ruleForm.userName = ele.name;
        },
        changeStandard(ele) {
            this.ruleForm.standardId = ele.id;
            this.ruleForm.standard = ele.checkName;
        },
        backPage() {
            this.$router.go(-1);
        },
        submitrule(formName) {

			// this.ruleForm.standards = str.substring(str.lastIndexOf("/")+1)
            this.$refs[formName].validate((valid) => {
                if (valid) {

                    let formdata = new FormData();
					var imgurls = this.$refs.uploadChang.fileList
                    // console.log(this.$refs.uploadChang.fileList)
					var arr=""
					for(var i=0;i<imgurls.length;i++){
					    if(imgurls[i].status != "success"){
					        formdata.append("files",imgurls[i].raw)
					    }
					   arr += imgurls[i].name + ';'
					}
					var str = arr.substr(0,arr.length-1)
					 this.ruleForm.detectionImg = str.substring(str.lastIndexOf("/")+1)
                    for (var a in this.ruleForm) {
                        if (this.ruleForm[a] == null || this.ruleForm[a] == 'null') {
                            this.ruleForm[a] = '';
                            formdata.append(a, '');
                        } else {
                            formdata.append(a, this.ruleForm[a]);
                        }
                    }
                    detection_save(formdata).then((res) => {
                        this.$notify({
                            title: '提示',
                            message: '保存成功',
                            type: 'success',
                            duration: 1000
                        });
                        this.$router.go(-1);
                    });
                } else {
                    return false;
                }
            });
        }
    }
};
</script>
<style scoped lang="less">
.delete_btn {
    margin-left: 6px;
}
</style>
